package troqler;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

@Named
@SessionScoped
public class BrukerBean implements Serializable {

    @Resource(name = "jdbc/waplj_prosjekt")
    private DataSource ds;
    private String passord, passord2;

    public String getBruker() {

        FacesContext fx = FacesContext.getCurrentInstance();
        ExternalContext ex = fx.getExternalContext();
        return ex.getRemoteUser();


    }

    public String getPassord() {
        return passord;
    }

    public void setPassord(String passord) {
        this.passord = passord;
    }

    public String getPassord2() {
        return passord2;
    }

    public void setPassord2(String passord2) {
        this.passord2 = passord2;
    }

    public String loggUt() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession httpSession = (HttpSession) facesContext.getExternalContext().getSession(true);
        httpSession.invalidate();
        HttpServletRequest req = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        /*try {
            req.logout();
        } catch (ServletException ex) {
            Logger.getLogger(BrukerBean.class.getName()).log(Level.SEVERE, null, ex);
        }*/
        return "/index";
    }

    public String byttPassord(String passord, String passord2) {
        FacesContext cx = FacesContext.getCurrentInstance();
        if (!passord.equals(passord2)) {
            FacesMessage msg = new FacesMessage();
            ResourceBundle bundle = cx.getApplication().getResourceBundle(cx, "msg"); 
            msg.setSummary(bundle.getString("likepassord"));

            cx.addMessage("byttpassord:passord", msg);
            return "";
        }
        String bruker = cx.getExternalContext().getRemoteUser();
        Connection con = null;
        PreparedStatement ps = null;

        try {
            con = ds.getConnection();
            ps = con.prepareStatement("UPDATE bruker set passord=? where brukernavn=?");
            ps.setString(1, passord);
            ps.setString(2, bruker);
            ps.executeUpdate();
            return "/okter/OktTabell";
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException ex) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                }
            }
            return "";
        }
    }
}
